package com.Utils;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;

public class HTMLProcessor {

    public static void main(String[] args) throws IOException {
        Path startDir = Paths.get("F:\\projectNow\\AAAProject\\huizhou3\\article"); // 要搜索的目录
        Files.walkFileTree(startDir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toString().toLowerCase().endsWith(".html")) {
                    try (Scanner scanner = new Scanner(file)) {
                        while (scanner.hasNextLine()) {
                            String line = scanner.nextLine();
                            if (line.contains("低空") || line.contains("无人机")) {
                                String path = file.toString().substring(0, file.toString().lastIndexOf("\\")) + "\\copy\\";
                                //判断是否存在目录，没用就创建
                                Path path1 = Paths.get(path);
                                if (!Files.exists(path1)) {
                                    Files.createDirectories(path1);
                                }
                                Path copyPath = Paths.get(path+file.getFileName());

                                Files.copy(file, copyPath, StandardCopyOption.REPLACE_EXISTING);
                                break;
                            }
                        }
                    }
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) {
                return FileVisitResult.CONTINUE;
            }
        });
    }
}